//
//  MainMenu.h
//  MainMenu: Main class of the game
//
//  Created by Enric Vergara on 20.10.12. 
//  Copyright OWLAB 2010. All rights reserved.
// Import the interfaces

#import "MainMenu.h"
#import "PokerTexasHoldem.h"
#import "Blackjack.h"
#import "SoundManager.h"


// CMainMenu implementation
@implementation CMainMenu

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	CMainMenu *layer = [CMainMenu node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if ((self = [super init]))
    {
        
        //---------GAMES MENU-------------------------------
        [CCMenuItemFont setFontName:@"Helvetica-BoldOblique"];
        [CCMenuItemFont setFontSize:60];
        
        
        m_Button_Poker = [CCMenuItemFont itemFromString:NSLocalizedString(@"PlayPoker", @"")
                                                 target:self 
                                               selector:@selector(menuPoker:)];
        
		m_Button_Poker.position =  CGPointMake(0, 0);
        
        m_Button_BlackJack = [CCMenuItemFont itemFromString:NSLocalizedString(@"PlayBlackJack", @"") 
                                                     target:self 
                                                   selector:@selector(menuBlackJack:)];
        
		m_Button_BlackJack.position =  CGPointMake(0, 120);
        
        
        CGSize screenSize = [[CCDirector sharedDirector] winSize];
        // create the menu using the items
        m_GamesMenu = [CCMenu menuWithItems:m_Button_Poker, m_Button_BlackJack, nil];
        m_GamesMenu.position = CGPointMake(screenSize.width/2, screenSize.height/2);
        [self addChild:m_GamesMenu];
        m_GamesMenu.visible = true;
        //------------------------------------------------------
        
        
        
        //---------SOUND MENU-------------------------------
        [CCMenuItemFont setFontName:@"Helvetica-BoldOblique"];
        [CCMenuItemFont setFontSize:36];
        m_Button_Sound = [CCMenuItemFont itemFromString:NSLocalizedString(@"SoundVolume", @"") 
                                                   target:self 
                                                 selector:@selector(menuSoundTouched:)];
		m_Button_Sound.position =  CGPointMake(0, 0);
        
        // create the menu using the items
        m_SoundTouch = [CCMenu menuWithItems:m_Button_Sound, nil];
        m_SoundTouch.position = CGPointMake(screenSize.width/2+ 300, screenSize.height/2+250);
        [self addChild:m_SoundTouch];

        
        m_SoundMenu = [CSoundMenu node];
        [self addChild:m_SoundMenu];
        m_SoundMenu.position = CGPointMake(screenSize.width/2, screenSize.height/2);
        m_SoundMenu.visible = false;
        //------------------------------------------------------
        
        
		// must be enabled if you want to receive touch events!
		self.isTouchEnabled = YES;
        
        [[SoundManager sharedInstance] playMusic:SOUND_MAIN_MENU_LOOP loop:true];
	}
    [self scheduleUpdate];
	return self;
}


-(void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    
    
    UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
        
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    CGRect rectangle5 = CGRectMake((screenSize.height/2)-100, (screenSize.width / 2)-100 , 200, 200);
    
    //Accio quan es clica al centre de la pantalla. Tornen a entrar les communityCards
    if(CGRectContainsPoint(rectangle5, location))
    {
        //[m_pBoard insertCommunityCardSprites];
    }
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
}

-(void) menuPoker: (id) sender
{
    if(m_SoundMenu.visible) return;
    
    [[SoundManager sharedInstance] stopMusic];
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_NEXT];
    //change scene
    [[CCDirector sharedDirector] replaceScene:[CPokerTexasHoldem scene]];
}

-(void) menuBlackJack: (id) sender
{
    if(m_SoundMenu.visible) return;
    
    [[SoundManager sharedInstance] stopMusic];
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_NEXT];
    //change scene
    [[CCDirector sharedDirector] replaceScene:[CBlackjack scene]];
}

- (void) menuSoundTouched: (id) sender
{
    if(m_SoundMenu.visible) return;
    
    [[SoundManager sharedInstance] playEffect:SOUND_BUTTON_SHOW_CARDS];
    m_SoundMenu.visible = true;
}

//----- UPDATE STATE MACHINE---
-(void) update:(ccTime)delta
{
    //something to do....
}

// on "dealloc" you need to release all your retained objects
-(void) dealloc
{
	//[m_Button_Poker release];
	//[m_Button_BlackJack release];
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
